package com.yq.bdm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yq.bdm.controller.common.AbstractController;
import com.yq.bdm.entity.MenuPermissionEntity;
import com.yq.bdm.service.MenuPermissionService;
import com.yq.bdm.utils.PageUtils;
import com.yq.bdm.utils.ResultMap;
import com.yq.bdm.utils.TreeList;

/**
 * 权限表
 * 
 * @author yuq
 * @date 2017-07-27 15:09:23
 */
@Controller
@RequestMapping("/menupermission")
public class MenuPermissionController extends AbstractController {

	@Autowired
	private MenuPermissionService menuPermissionService;

	@RequestMapping("/index.html")
	public String list() {
		return "admin/menupermission/menupermission.html";
	}

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public ResultMap list(String id, String permissionId,
			String parentPermissionId, String permissionName, String icon,
			String url, String description, String sort, String isVisible,
			String gmtCreate, String gmtModified) {

		Map<String, Object> map = new HashMap<>();
		// 查询条件
		map.put("id", id);
		map.put("permissionId", permissionId);
		map.put("parentPermissionId", parentPermissionId);
		map.put("permissionName", permissionName);
		map.put("icon", icon);
		map.put("url", url);
		map.put("description", description);
		map.put("sort", sort);
		map.put("isVisible", isVisible);
		map.put("gmtCreate", gmtCreate);
		map.put("gmtModified", gmtModified);

		// 获取分页参数，添加到查询请求参数中
		getPage(map);

		// 查询列表数据
		List<MenuPermissionEntity> menuPermissionList = menuPermissionService
				.queryList(map);
		int total = menuPermissionService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(menuPermissionList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/treeList")
	public ResultMap treeList() {

		// 查询列表数据
		List<MenuPermissionEntity> menuPermissionList = menuPermissionService
				.queryList(null);
		return ResultMap.ok().put("list", menuPermissionList);
	}
	
	/**
	 * 后台管理页面左侧菜单树
	 */
	@ResponseBody
	@RequestMapping("/mainMenuTreList")
	public ResultMap mainMenuTreList() {

		// 查询列表数据
		List<MenuPermissionEntity> menuPermissionList = menuPermissionService
				.queryList(null);
        TreeList treeList = new TreeList();
        List<MenuPermissionEntity> returnMenuTree = treeList.testQueryMenuList(menuPermissionList);
		return ResultMap.ok().put("list", returnMenuTree);
	}
	
	/**
	 * 跳转到新增页面
	 */
	@RequestMapping("/goAdd")
	public String goAdd() {
		return "admin/menupermission/add.html";
	}

	/**
	 * 跳转到编辑页面
	 */
	@RequestMapping("/goEdit/{id}")
	public String goEdit(@PathVariable("id") Long id) {
		// 设置当前control的访问url，edit页面需要使用
		request.setAttribute("namespaceUrl",
				request.getContextPath() + "/menupermission");
		MenuPermissionEntity menuPermission = menuPermissionService
				.queryObject(id);
		request.setAttribute("menuPermission", menuPermission);
		return "admin/menupermission/edit.html";
	}

	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{permissionId}")
	public ResultMap info(@PathVariable("permissionId") Long permissionId) {
		MenuPermissionEntity menuPermission = menuPermissionService
				.queryObjectByPermissionId(permissionId);

		return ResultMap.ok().put("menuPermission", menuPermission);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	public ResultMap save(@RequestBody MenuPermissionEntity menuPermission) {
		menuPermissionService.save(menuPermission);

		return ResultMap.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public ResultMap update(@RequestBody MenuPermissionEntity menuPermission) {
		menuPermissionService.update(menuPermission);

		return ResultMap.ok();
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public ResultMap delete(@RequestBody Long[] ids) {
		menuPermissionService.deleteBatch(ids);

		return ResultMap.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/deleteByPermissionId")
	public ResultMap deleteByPermissionId(@RequestBody Long[] permissionIds) {
		menuPermissionService.deleteBatchByPermissionId(permissionIds);

		return ResultMap.ok();
	}

	/**
	 * 删除成功后，查询列表的数据
	 */
	@ResponseBody
	@RequestMapping("/deleteAfterList")
	public ResultMap deleteAfterList(@RequestBody Long[] ids) {
		menuPermissionService.deleteBatch(ids);

		Map<String, Object> map = new HashMap<>();
		// 获取分页参数，添加到查询请求参数中
		getPage(map);
		// 查询列表数据
		List<MenuPermissionEntity> menuPermissionList = menuPermissionService
				.queryList(map);
		int total = menuPermissionService.queryTotal(map);
		PageUtils pageUtil = new PageUtils(menuPermissionList, total, map);
		return ResultMap.ok().put("page", pageUtil);
	}
}
